<?php
namespace app\backend\model;

use \think\Db;
use traits\model\SoftDelete;
use app\backend\model\Tab as TabModel;

/**
 * 左侧菜单栏模型类
 */
class Menu extends \think\Model
{
    //软删除
    use SoftDelete;
    static protected $deleteTime = 'delete_time';

    // 获取并返回左侧菜单栏数据，同时设置缓存
    public static function menuData()
    {
        $funlltime_menu = "领星产品";
        //循环遍历menu表和tab表，组装数据，拼接子类
        $menu_name_type = (\think\Session::get('language') == 'en') ? 'menu_name_en' : 'menu_name_cn';
        $menu_data = Db::name('menu')->field(['menu_id', 'p_id', 'controller', 'action', 'urlflag','menu_url','type_id', $menu_name_type =>'menu_name'])->where(["status"=>1,"delete_time"=>['exp','is null']])->order(['p_id'=>'ASC', 'sort'=>'ASC'])->select();
//        $tab_data = Db::name('tab')->field(["tab_id","p_id","tab_name_cn","tab_url"])->where(["status"=>1,"delete_time"=>['exp','is null']])->select();

        $people_sub_data = Db::name('team')->field(['team_id', 'team_name_cn' => 'menu_name'])->where(["delete_time"=>['exp','is null']])->select();
        $type_data = Db::name('type')->field(['type_id', 'remark_cn'=>'menu_name'])->where(["delete_time"=>['exp','is null'],"type_id"=>["exp","in(6,7)"]])->select();
//var_dump($type_data);exit;

        //查找联系我们的位置信息
        $add_data = Db::name('contact')->field(['type_id','con_id', 'contact_cn' => 'menu_name'])->where(["type_id"=>1,"delete_time"=>['exp','is null']])->select();


        // 文章管理菜单，把康x源 放到“全时全程”下
        $menu_data1=$menu_data;
        $temp_arr = [];
        foreach ($menu_data as $k=>$v){
            if($v["menu_name"]==$funlltime_menu){
                foreach ($menu_data1 as $c=>$d){
                    if($d["p_id"]==$v["menu_id"] && $d["type_id"]==2){
                        $d["urlflag"] = 1;
                        $d["menu_url"] = '/backend/lists/index/type/articles/id/'.$d['menu_id'];
                        $temp_arr[] = $d['menu_id'];
                        if (isset($menu_data[$k]["child"])) {
                            $menu_data[$k]["child"][] = $d;
                        } else {
                            $menu_data[$k]["child"] = [$d];
                        }
                    }/*elseif ($d["menu_name"]=="铂金版"){
                        $temp_arr[] = $d['menu_id'];
                    }*/
                }
                break;
            }
        }

        // 去除 $menu_data 中的 康x源
        foreach ($menu_data as $k=>$v){
            if(in_array($v['menu_id'], $temp_arr)){
                unset($menu_data[$k]);
            }
        }
//        return $menu_data;exit;

        //新闻管理文章管理的左边栏
        $menu_data1=$menu_data;
        foreach ($menu_data as $k=>$v){
            foreach ($menu_data1 as $a=>$b){
                if($b['menu_name'] == $funlltime_menu){
                    $b['urlflag'] = -1;
                }
               if(strpos($b["type_id"],$v["type_id"]) !==false && $v["p_id"]<$b["p_id"] && $b["type_id"] !=0 && $v["type_id"] !=0){
                   if($v["type_id"]==1){//1 新闻
                       $url ='news';
                   }elseif ($v["type_id"]==2){//2 文章
                       $url ='articles';
                   }
                   $b["menu_url"] = "/backend/lists/index/type/$url/id/".$b["menu_id"];
                   if (isset($menu_data[$k]["child"])) {
                       $menu_data[$k]["child"][] = $b;
                   } else {
                       $menu_data[$k]["child"] = [$b];
                    }
               }
            }
        }
//        return $menu_data;exit;

        // 去除 $menu_data 中"全时全程"的下的 child 数组： 康x源
        foreach ($menu_data as $k=>$v){
            if($v['menu_name'] == $funlltime_menu){
                unset($menu_data[$k]['child']);
                break;
            }
        }
//        return $menu_data;exit;

        /*foreach ($menu_data as $k=>$v){
            foreach ($tab_data as $a=>$b){
                if($v["menu_id"]==$b["p_id"]){
                    // $menu_data[$k]["child"][]=$b;
                    //array_push($menu_data[$k]["child"],$b);
                    if (isset($menu_data[$k]["child"])) {
                        //array_push($menu_data[$k]["child"],$b);
                        $menu_data[$k]["child"][] = $b;
                    } else {
                        $menu_data[$k]["child"] = [$b];
                    }
                }
            }
        }*/
//        return $menu_data;exit;
        $menu =self::_get_child($menu_data);

        foreach ($menu as $k=>$v){
            //人员管理下面加上团队分类
            if ($v["menu_name"]=="人员管理"){
                foreach ($people_sub_data as $k1=>$v1){
                    $people_sub_data[$k1]['menu_id'] = $v1['team_id'];
                    $people_sub_data[$k1]['menu_url'] = '/backend/people/index/team_id/'.$v1['team_id'];
                    $people_sub_data[$k1]['urlflag'] = '1';
                }
                $menu[$k]['child'] = $people_sub_data;
            }

            //首页管理加上首页分类
            if ($v["menu_name"]=="首页管理"){

                foreach ($type_data as $k2=>$v2){
                    if($v2["type_id"]==6){
                        $type_data[$k2]['menu_id'] = $v2['type_id'];
                        $type_data[$k2]['menu_url'] = '/backend/index/index/type_id/'.$v2['type_id'];
                        $type_data[$k2]['urlflag'] = '1';
                    }elseif ($v2["type_id"]==7){
                        $type_data[$k2]['menu_id'] = $v2['type_id'];
                        $type_data[$k2]['menu_url'] = '/backend/brand/index/type_id/'.$v2['type_id'];
                        $type_data[$k2]['urlflag'] = '1';
                    }
                }
                $menu[$k]['child'] = $type_data;
                //break;
            }

            //联系我们加上位置信息
            if ($v["menu_name"]=="联系我们"){
                //var_dump($add_data);exit;
                foreach ($add_data as $k3=>$v3){
                    if($v3["type_id"]==1){
                        $add_data[$k3]['menu_id'] = $v3['con_id'];
                        $add_data[$k3]['menu_url'] = '/backend/contact/index/con_id/'.$v3['con_id'];
                        $add_data[$k3]['urlflag'] = '1';
                    }
                }

                $menu[$k]['child'] = $add_data;
                //break;
            }

        }

        /*header("content-type:text/html;charset=utf-8");
        echo "<pre>";
        print_r($menu);exit;*/

        return $menu;
    }

    public function getnav(){
        // 查询数据
        $data =$this
            ->field('menu_id,menu_name_cn,menu_name_en,p_id,menu_url')
            ->where(array('status'=>1,'delete_time'=>['exp','is null']))
            ->select()
            // ->where("p_id = $pid")
            /*->paginate(5)*/;
//self对类自己本身的引用
        $arr=self::_get_child($data); //得到菜单数组
        //echo "<pre>";
        //print_r($arr);exit;
        //$json=json_encode($arr);
        return $arr; //返回json格式
    }


    //分类级别
    static protected function _cate_level($data, $pid=0, $level=0){
        $array = array();
        foreach ($data as $k => $v){
            if ($v['p_id'] == $pid){
                $v['level'] = $level;
                $array[] = $v;
                unset($data[$k]);
                $array = array_merge($array, self::_cate_level($data, $v['menu_id'],$level+1)); //递归
            }
        }
        return $array;
    }

    //模块管理index展示
    public function getAll()
    {
        $data=$this
            ->field('menu_id,p_id,menu_name_cn,menu_name_en,sort')
            ->where(["p_id"=>array('exp','<>0')])
            ->where(["p_id"=>1])
            ->order('sort','ASC')
            ;
            //->order("is_recommend DESC");
        //->select();
        return $data;
    }

    public function menu_2($id)
    {
        $data=$this
            ->field('menu_id,p_id,menu_name_cn,menu_name_en')
            ->where(["p_id"=>array('exp','<>0')])
            ->where(["p_id"=>"$id"])//$id不等于0
        ;
        //->order("is_recommend DESC");
        //->select();
        return $data;
    }


    //菜单
    static protected function _get_child($data, $pid=0){
        $array = array();
        foreach ($data as $k => $v){
            if ($v['p_id'] == $pid){
                //$v['child'] = self::_get_child($data, $v['menu_id']); //递归
                if (!array_key_exists('child', $v)) {//判断$v里面有没有child这个key
                    $v['child'] = self::_get_child($data, $v['menu_id']); //递归
                }
                $array[] = $v;
            }
        }
        return $array;
    }

    public static function nav2($data, $pid=1){
        $array = array();
        foreach ($data as $k => $v){
            if ($v['p_id'] == $pid){
                //$v['child'] = self::_get_child($data, $v['menu_id']); //递归
                if (!array_key_exists('child', $v)) {//判断$v里面有没有child这个key
                    $v['child'] = self::nav2($data, $v['menu_id']); //递归
                }
                $array[] = $v;
            }
        }
        return $array;
    }




}